import * as _ from "lodash"
import httpClient from "../httpclient"

export class Menu {
  public id: string
  public icon: string
  public name: string
  public url: string
  public children: Menu[]
  public position: number
}

function assignMenu(menu: Menu) {
  return _.assign(new Menu(), menu, {
    children: _.map(menu.children, assignMenu),
  })
}

export class MenuService {
  public static getMenu(id: string): Promise<Menu> {
    return httpClient.get(`/v1/menus/${id}`)
      .then(({ data }) => data).then(assignMenu)
  }
}
